package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void saveEmpAndEmpExprList(Emp emp) {
        LocalDateTime now = LocalDateTime.now();
        emp.setCreateTime(now);
        emp.setUpdateTime(now);
        empMapper.insert(emp);
        // 保存员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(o -> o.setEmpId(emp.getId()));
        empExprMapper.insert(exprList);
    }

    @Override
    public PageResult<Emp> getConditionPageList(EmpQueryParam param) {
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        // 调用Service层查询分页数据
        LambdaQueryWrapper<Emp> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(param.getName()), Emp::getName, param.getName())
                .eq(ObjUtil.isNotNull(param.getGender()), Emp::getGender, param.getGender())
                .between(ObjUtil.isNotNull(param.getBegin()) && ObjUtil.isNotNull(param.getEnd()), Emp::getEntryDate, param.getBegin(), param.getEnd());
        this.page(page, qw);
        List<Emp> empList = page.getRecords();
        // 查询部门信息封装id，name为Map集合
        Map<Integer, String> depts = deptMapper.selectList(null).stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
        // 设置员工deptName
        empList.forEach(o -> o.setDeptName(depts.get(o.getDeptId())));
        return new PageResult<>(page.getTotal(), empList);
    }

    @Override
    public Emp getEmpAndExprById(Integer id) {
        Emp emp = empMapper.selectById(id);
        List<EmpExpr> empExprs = empExprMapper.selectList(Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, id));
        emp.setExprList(empExprs);
        return emp;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateEmpAndEmpExprById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        // 修改员工基本信息
        LambdaUpdateWrapper<Emp> luw = new LambdaUpdateWrapper<>();
        luw.eq(Emp::getId, emp.getId());
        empMapper.update(emp, luw);
        // 删除员工工作经历
        empExprMapper.delete(Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, emp.getId()));
        // 添加员工工作经历
        if(CollUtil.isNotEmpty(emp.getExprList())){
            empExprMapper.insert(emp.getExprList());
        }
    }

    @Override
    public void deleteEmpAndExprByIds(List<Integer> ids) {
        // 删除员工基本信息
        empMapper.deleteByIds(ids);
        // 删除员工工作经历
        empExprMapper.delete(Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId, ids));
    }
}
